#statsNamespace.py
#Collects stats from files of namespace by startyear
#
#Health of Online Communities

import os
import sys
import collections
import multiprocessing

def main():
	inputPath = sys.argv[1]
	outputPath = sys.argv[2]
	inputFiles = os.listdir(inputPath) #all files in inputPath

	workers = []

	for nsfile in inputFiles:
		inputFileName = inputPath+nsfile
		outputFileName = outputPath+nsfile

		worker = StatWorker(inputFileName, outputFileName)
		workers.append(worker)
		worker.start

	for worker in workers:
		worker.join()

class StatWorker(multiprocessing.Process):
	
	def __init__(self, inputFileName, outputFileName):
		multiprocessing.Process.__init__(self):
		self.inputFileName = inputFileName
		self.outputFileName = outputFileName

	def run(self):
		stats(self.inputFileName, self.outputFileName)

#prints out the facts
def stats(inputFileName, outputFileName):
	inputFileHandle = open(inputFileName,"r")
	outputFileHandle = open(outputFileName, "a+");
	
	statDict = collections.defaultdict(int)
	outputFileHandle.write("Stats for " + inputFileName+"\n")
	
	for line in inputFileHandle:
		pieces = line.split('\t')
		year = pieces[1].split('-')[0]
		startyear = pieces[3].split('-')[0]
		statDict[int(year)-int(startyear)] += 1 #edits by offset year
		statDict["TOTAL"] += 1 #all edits done in namespace by user who started in start year (lines in file)
	for y in sorted(statDict.keys()):
		outputFileHandle.write(str(y)+" "+str(statDict[y])+"\n") #prints offset year followed by # of edits
	
	inputFileHandle.close()
	outputFileHandle.close()

main()
